zabbix Monitoring docker container state [Recommended]

  • 2020-11-20 06:21:09
  • OfStack

Some time ago, when I was deploying zabbix, I had a need to monitor the state of the container, that is, the occupancy of cpu memory, so I wrote a script and template by myself, and I share 1 here.

I'm not going to say any more nonsense

First choice, zabbix_agentd configuration vim usr/local zabbix/etc/zabbix_agentd conf


UserParameter=docker.discovery,/usr/local/zabbix/script/docker.py
UserParameter=docker.[*],/usr/local/zabbix/script/docker.py $1 $2

Here is the ES22en.py script, which USES an automatic discovery rule to discover the container, and then specifies that the container gets the status information


#!/usr/bin/python
import sys
import os
import json
def discover():
 d = {}
 d['data'] = []
 with os.popen("docker ps -a --format {{.Names}}") as pipe:
  for line in pipe:
   info = {}
   info['{#CONTAINERNAME}'] = line.replace("\n","")
   d['data'].append(info)
 print json.dumps(d)
def status(name,action):
 if action == "ping":
  cmd = 'docker inspect --format="{{.State.Running}}" %s' %name
  result = os.popen(cmd).read().replace("\n","")
  if result == "true":
   print 1
  else:
   print 0
 else:
  cmd = 'docker stats %s --no-stream --format "{{.%s}}"' % (name,action)
  result = os.popen(cmd).read().replace("\n","")
  if "%" in result:
   print float(result.replace("%",""))
  else:
   print result
if __name__ == '__main__':
  try:
    name, action = sys.argv[1], sys.argv[2]
    status(name,action)
  except IndexError:
    discover()

It says here that 1 automatic discovery of regular pits... It took me a long time to find out... 1 must return json, 2 info['{#CONTAINERNAME}'] this key1 must say {#CONTAINERNAME}...

The result is as follows, 1 must be such a hierarchical relationship...


{"data": [{"{#CONTAINERNAME}": "node-3"}, {"{#CONTAINERNAME}": "node-2"}, {"{#CONTAINERNAME}": "node-1"}, {"{#CONTAINERNAME}": "web"}, {"{#CONTAINERNAME}": "cadvisor"}, {"{#CONTAINERNAME}": "updatol"}, {"{#CONTAINERNAME}": "research"}, {"{#CONTAINERNAME}": "services"}, {"{#CONTAINERNAME}": "data"}, {"{#CONTAINERNAME}": "rabbitmq"}, {"{#CONTAINERNAME}": "redis"}, {"{#CONTAINERNAME}": "mysql"}, {"{#CONTAINERNAME}": "ssdb"}]}

The other function is a simple call to the docker command to fetch the data.

Automatic discovery rules and that's it

These are the only monitored states, and there is one trigger, ping, that detects if the current container state is running and alerts if it is not.

The template as follows


<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
 <version>3.2</version>
 <date>2018-06-04T04:12:36Z</date>
 <groups>
  <group>
   <name>Templates</name>
  </group>
 </groups>
 <templates>
  <template>
   <template>docker-status</template>
   <name>docker-status</name>
   <description/>
   <groups>
    <group>
     <name>Templates</name>
    </group>
   </groups>
   <applications>
    <application>
     <name>docker_test</name>
    </application>
   </applications>
   <items/>
   <discovery_rules>
    <discovery_rule>
     <name>docker.discovery</name>
     <type>0</type>
     <snmp_community/>
     <snmp_oid/>
     <key>docker.discovery</key>
     <delay>60</delay>
     <status>0</status>
     <allowed_hosts/>
     <snmpv3_contextname/>
     <snmpv3_securityname/>
     <snmpv3_securitylevel>0</snmpv3_securitylevel>
     <snmpv3_authprotocol>0</snmpv3_authprotocol>
     <snmpv3_authpassphrase/>
     <snmpv3_privprotocol>0</snmpv3_privprotocol>
     <snmpv3_privpassphrase/>
     <delay_flex/>
     <params/>
     <ipmi_sensor/>
     <authtype>0</authtype>
     <username/>
     <password/>
     <publickey/>
     <privatekey/>
     <port/>
     <filter>
      <evaltype>0</evaltype>
      <formula/>
      <conditions>
       <condition>
        <macro>{#CONTAINERNAME}</macro>
        <value>@ CONTAINER NAME</value>
        <operator>8</operator>
        <formulaid>A</formulaid>
       </condition>
      </conditions>
     </filter>
     <lifetime>30</lifetime>
     <description/>
     <item_prototypes>
      <item_prototype>
       <name>Container {#CONTAINERNAME} Diskio usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME} ,BlockIO]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>0</trends>
       <status>0</status>
       <value_type>1</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container{#CONTAINERNAME} CPU usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},CPUPerc]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>0</value_type>
       <allowed_hosts/>
       <units>%</units>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container {#CONTAINERNAME} mem usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},MemPerc]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>0</value_type>
       <allowed_hosts/>
       <units>%</units>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container {#CONTAINERNAME} NETio usage:</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME},NetIO]</key>
       <delay>60</delay>
       <history>90</history>
       <trends>0</trends>
       <status>0</status>
       <value_type>1</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
      <item_prototype>
       <name>Container{#CONTAINERNAME} is_run :</name>
       <type>0</type>
       <snmp_community/>
       <multiplier>0</multiplier>
       <snmp_oid/>
       <key>docker.[{#CONTAINERNAME} ,ping]</key>
       <delay>30</delay>
       <history>90</history>
       <trends>365</trends>
       <status>0</status>
       <value_type>3</value_type>
       <allowed_hosts/>
       <units/>
       <delta>0</delta>
       <snmpv3_contextname/>
       <snmpv3_securityname/>
       <snmpv3_securitylevel>0</snmpv3_securitylevel>
       <snmpv3_authprotocol>0</snmpv3_authprotocol>
       <snmpv3_authpassphrase/>
       <snmpv3_privprotocol>0</snmpv3_privprotocol>
       <snmpv3_privpassphrase/>
       <formula>1</formula>
       <delay_flex/>
       <params/>
       <ipmi_sensor/>
       <data_type>0</data_type>
       <authtype>0</authtype>
       <username/>
       <password/>
       <publickey/>
       <privatekey/>
       <port/>
       <description/>
       <inventory_link>0</inventory_link>
       <applications>
        <application>
         <name>docker_test</name>
        </application>
       </applications>
       <valuemap/>
       <logtimefmt/>
       <application_prototypes/>
      </item_prototype>
     </item_prototypes>
     <trigger_prototypes>
      <trigger_prototype>
       <expression>{docker-status:docker.[{#CONTAINERNAME} ,ping].last()}=0</expression>
       <recovery_mode>0</recovery_mode>
       <recovery_expression/>
       <name>docker_{#CONTAINERNAME}_down</name>
       <correlation_mode>0</correlation_mode>
       <correlation_tag/>
       <url/>
       <status>0</status>
       <priority>5</priority>
       <description/>
       <type>0</type>
       <manual_close>0</manual_close>
       <dependencies/>
       <tags/>
      </trigger_prototype>
     </trigger_prototypes>
     <graph_prototypes/>
     <host_prototypes/>
    </discovery_rule>
   </discovery_rules>
   <httptests/>
   <macros/>
   <templates/>
   <screens/>
  </template>
 </templates>
</zabbix_export>

Modify the Zabbix_agentd configuration, docker.py script to be placed in the specified path, don't forget to give permission, import the template, it is ok to get the data. Can not get, zabbix_get to debug to find out where the problem to solve.

conclusion


Related articles: